<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 设置权限 控制层
 * @author adela.xu 8.29
 */
class Menu extends My_Controller
{
    private $assign = array();
    /**
     * 默认方法
     * @author Toby.tu 2016-09-01
     */
    public function init(){
        $this->load->model('Menu_model','menu_model');
    }
    /**
     * 用户列表
     * @author Toby.tu 2016-08-30
     */
    public function index(){
        $menus = $this->menu_model->getAll();
        $menu_list = array();
        foreach ($menus as $menu) {
            if($menu['parent_id'] > 0){
                $menu_list[$menu['parent_id']]['list'][$menu['id']]['info'] = $menu;
            }else{
                $menu_list[$menu['id']]['info'] = $menu;
            }
        }
        $this->assign['menu_list'] = $menu_list;
        $this->assign['view'] = 'admin/menu/index';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function create($id=0){
        if($id > 0){
            $menu = $this->menu_model->getOne(['id'=>$id]);
        }else{
            $menu = array();
        }
        $this->assign['menu'] = $menu;
        $this->assign['id'] = $id;
        //获取是否菜单展示选项
        $this->vendor('@.Picklist_model');
        $is_showmenu_opts = $this->picklist_model->getOptsByName('is_showmenu');
        $this->assign['is_showmenu_opts'] = $is_showmenu_opts;
        $this->assign['is_showmenu_label'] = current($is_showmenu_opts)['label'];
        $this->assign['view'] = 'admin/menu/create';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function store(){
        try {
            $input = $this->input->post();
            if(!isset($input['menu'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['menu']['name'])){
                throw new Exception('请输入菜单姓名。');
            }
            $menu = $this->menu_model->getOne(['name'=>$input['menu']['name']]);
            if(!empty($menu)){
                throw new Exception('菜单名已存在。');
            }
            $param = $input['menu'];
            $param['type'] = 1;
            $this->menu_model->newData($param);
            redirect('admin/Menu/index');die;
        } catch (Exception $e) {
            $this->assign += $input['menu'];
            $this->assign['error_code'] = $e->getMessage();
            if(isset($input['menu']['parent_id'])){
                $this->assign['id'] = $input['menu']['parent_id'];
            }else{
                $this->assign['id'] = 0;
            }
            if($this->assign['id'] > 0){
                $menu = $this->menu_model->getOne(['id'=>$this->assign['id']]);
            }else{
                $menu = array();
            }
            $this->assign['menu'] = $menu;
            //获取是否菜单展示选项
            $this->vendor('@.Picklist_model');
            $is_showmenu_opts = $this->picklist_model->getOptsByName('is_showmenu');
            $this->assign['is_showmenu_opts'] = $is_showmenu_opts;
            $this->assign['is_showmenu_label'] = current($is_showmenu_opts)['label'];
            $this->assign['view'] = 'admin/menu/create';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 创建页面
     * @author Toby.tu 2016-08-31
     */
    public function edit($id){
        $menu = $this->menu_model->getOne(['id'=>$id]);
        unset($menu['status']);
        $this->assign += $menu;
        $this->assign['id'] = $id;
        if($menu['parent_id'] > 0){
            $parent = $this->menu_model->getOne(['id'=>$menu['parent_id']]);
        }else{
            $parent = array();
        }
        $this->assign['parent'] = $parent;
        //获取是否菜单展示选项
        $this->vendor('@.Picklist_model');
        $is_showmenu_opts = $this->picklist_model->getOptsByName('is_showmenu');
        $this->assign['is_showmenu_opts'] = $is_showmenu_opts;
        $this->assign['is_showmenu_label'] = current($is_showmenu_opts)['label'];
        $this->assign['view'] = 'admin/menu/edit';
        $this->load->view('admin/common/frame',$this->assign);
    }
    /**
     * 新增权限组
     * @author Toby.tu 2016-08-31
     */
    public function update($id){
        try {
            $input = $this->input->post();
            if(!isset($input['menu'])){
                throw new Exception('参数不正确。');
            }
            if(empty($input['menu']['name'])){
                throw new Exception('请输入菜单姓名。');
            }
            $menu = $this->menu_model->getOne(['name'=>$input['menu']['name']]);
            if(!empty($menu) && $id != $menu['id']){
                throw new Exception('菜单名已存在。');
            }
            $param = $input['menu'];
            if($param['parent_id'] > 0 && !$param['type']){
                $param['type'] = 0;
            }
            $this->menu_model->updateById($id,$param);
            redirect('admin/menu/index');die;
        } catch (Exception $e) {
            $this->assign += $input['menu'];
            $this->assign['error_code'] = $e->getMessage();
            $menu = $this->menu_model->getOne(['id'=>$id]);
            $this->assign['id'] = $id;
            if($menu['parent_id'] > 0){
                $parent = $this->menu_model->getOne(['id'=>$menu['parent_id']]);
            }else{
                $parent = array();
            }
            $this->assign['parent_id'] = $menu['parent_id'];
            $this->assign['parent'] = $parent;
            //获取是否菜单展示选项
            $this->vendor('@.Picklist_model');
            $is_showmenu_opts = $this->picklist_model->getOptsByName('is_showmenu');
            $this->assign['is_showmenu_opts'] = $is_showmenu_opts;
            $this->assign['is_showmenu_label'] = current($is_showmenu_opts)['label'];
            $this->assign['view'] = 'admin/menu/edit';
            $this->load->view('admin/common/frame',$this->assign);
        }
    }
    /**
     * 禁用启用
     * @author Toby.tu 2016-08-31
     */
    public function enable($id){
        try {
            $input = $this->input->get();
            if(!isset($input['status'])){
                throw new Exception('参数不正确。');
            }
            $param = array();
            $param['status'] = ($input['status']==1)?0:1;
            $this->menu_model->updateById($id,$param);
            $menu = $this->menu_model->getOne(['id'=>$id]);
            if($menu['parent_id'] == 0){
                $this->menu_model->db->where_in('parent_id',$id)
                    ->update($this->menu_model->tab, $param);
            }
            redirect('admin/Menu/index');die;
        } catch (Exception $e) {
            redirect('admin/Menu/index');die;
        }
    }
    /**
     * 删除
     * @author Toby.tu 2016-08-31
     */
    public function destroy($id){
        $this->user_model->deleteById($id);
        redirect('admin/User/index');die;
    }
}
